import java.io.*;
import java.util.*;

public class TransactionList extends ArrayList<Transaction> {

    private DescendingIDComparator descendingIDComparator;
    private AscendingMemberIDComparator ascendingMemberIDComparator;
    private DescendingMemberIDComparator descendingMemberIDComparator;
    private AscendingItemIDComparator ascendingItemIDComparator;
    private DescendingItemIDComparator descendingItemIDComparator;

    public TransactionList() {
	super();
	descendingIDComparator = new DescendingIDComparator();
	ascendingMemberIDComparator = new AscendingMemberIDComparator();
	descendingMemberIDComparator = new  DescendingMemberIDComparator();
	ascendingItemIDComparator = new AscendingItemIDComparator();
	descendingItemIDComparator = new DescendingItemIDComparator();
    }

public String toString() {
	String s = "";

	s += ("\n\n");
	s += ("                                     TRANSACTIONS");
	s += ("\n----------------------------------------------------------------------------------------\n");
	s += String.format("%-15s %-10s %-21s %-11s %-10s","TransactionID","MemberID", "ItemID","Quantity","Date");
	s += ("\n----------------------------------------------------------------------------------------\n");
	for(int i = 0; i < size(); i++) {
	    s += String.format("%-15s %-10s %-21s %-11s %-10s",get(i).transactionID,get(i).getMemberID(), get(i).getItemID(), get(i).getQuantity(),get(i).getDate());
	    s += "\n";
	}
	return s;
    }

    public void sortByIDA() {
	Collections.sort(this);
    }

    public void sortByIDD() {
	Collections.sort(this, descendingIDComparator);
    }

  public void sortByMemberIDA() {
      Collections.sort(this, ascendingMemberIDComparator);
    }

    public void sortByMemberIDD() {
	Collections.sort(this, descendingMemberIDComparator);
    }
  public void sortByItemIDA() {
      Collections.sort(this, ascendingItemIDComparator);
    }

    public void sortByItemIDD() {
	Collections.sort(this, descendingItemIDComparator);
    }

    public class AscendingMemberIDComparator implements Serializable, Comparator<Transaction>{
	public int compare(Transaction a, Transaction b) {
	    return (a.getMemberID() - b.getMemberID());
	}
    }

    public class DescendingMemberIDComparator implements Serializable, Comparator<Transaction>{
	public int compare(Transaction a, Transaction b) {
	    return (b.getMemberID() - a.getMemberID());
	}
    }

    public class DescendingIDComparator implements Serializable, Comparator<Transaction>{
	public int compare(Transaction a, Transaction b) {
	    return (b.transactionID - a.transactionID);
	}
    }

    public class AscendingItemIDComparator implements Serializable, Comparator<Transaction>{
	public int compare(Transaction a, Transaction b) {
	    return (a.getItemID() - b.getItemID());
	}
    }

    public class DescendingItemIDComparator implements Serializable, Comparator<Transaction>{
	public int compare(Transaction a, Transaction b) {
	    return (b.getItemID() - a.getItemID());
	}
    }
}